package com.kaigejava.juc.volatitle;

/**
 * @author kaigejava
 * 多线程下懒汉式的单例
 */
public class SingletonDemo2 {
    private static SingletonDemo2 instance = null;
    private SingletonDemo2(){
        System.out.println("======开始调用私有构造器了。线程名称："+Thread.currentThread().getName());
    }
    public  static synchronized SingletonDemo2 getInstance(){
        if(null == instance){
            instance = new SingletonDemo2();
        }
        return instance;
    }
    public static void main(String[] args) {
        for(int i = 0;i<10;i++){
            new Thread(()->{
                SingletonDemo2.getInstance();
            },"线程"+(i+1)).start();
        }
    }
}
